package com.winchain.provider.expiration.cmd;

import com.shell.framework.command.CommandContext;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.XfeRevTimeoutKeyword;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.base.service.CbXfeService;
import com.winchain.provider.expiration.rx.BillExpirationEvent;
import com.winchain.provider.expiration.service.BillExpQueryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillExpDlySettlCmd extends BillExpChdSettlCmd {
	@Autowired
	CusRecService basCusService;
	@Autowired
	CbXfeService cbXfeService;

	public boolean execute(CommandContext<BillExpirationEvent> context) throws Exception {
		BillExpirationEvent event = (BillExpirationEvent) context.getSource();
		String expDate = event.getDate();
		boolean pm = event.isPm();
		String grpId = event.getGrpId();

		List<CbRec> bills = this.billExpQueryService.selectExpDlyBills(expDate, grpId);
		for (CbRec bill : bills) {
			if (bill.getExpDate().equals(expDate)) {
				delayProcess(bill, expDate, pm);
			}
		}
		return false;
	}

	public void sendMessage(CbRec bill) throws Exception {
		CbXfe cbXfe = new CbXfe();
		cbXfe.setBillId(bill.getBillId());
		CbXfe data = (CbXfe) this.cbXfeService.selectOne(cbXfe);

		String template = "MT01403";
		XfeRevTimeoutKeyword keyWord = new XfeRevTimeoutKeyword();
		keyWord.setBillAmt(String.valueOf(bill.getBillAmt()));
		keyWord.setSellerNm(data.getSellerNm());
		keyWord.setXfeDate(data.getCreateTime());

		String senderId = CommonDataUtil.getUserId();

		String buyerId = data.getBuyerId();
		MessageSender.sendMessageByTmpl(senderId, template, keyWord, buyerId);
	}
}
